﻿using BtLabelModel;
using BtLabelTool;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media;

namespace BtLabelBLL
{
    public class RelayUartBLL
    {
        public SerialPort port;
        public RichTextBox _RichTextBoxInfo;
        /// <summary>
        /// 一发一收
        /// </summary>
        /// <param name="sendstr">发送数据</param>
        /// <param name="delay">返回超时</param>
        /// <param name="wait">返回等待</param>
        /// <returns>返回数据</returns>
        public string SendGetStr(string sendstr, int delay = 2000, int wait = 100)
        {
            try
            {
                PortHelper.SendData(port, sendstr);
                string senddata = DateTime.Now.ToString("HH:mm:ss.fff") + "\r\nSend:" + sendstr;
                UiBLL.RichTextBoxAppendText(_RichTextBoxInfo, Brushes.SteelBlue, senddata);
                string recievestr = PortHelper.GetData(port, delay, wait);
                string recievedata = DateTime.Now.ToString("HH:mm:ss.fff") + "\r\nRecieve:" + recievestr;
                UiBLL.RichTextBoxAppendText(_RichTextBoxInfo, Brushes.SteelBlue, recievedata);
                return recievestr;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 关闭通道串口继电器
        /// </summary>
        /// <param name="channel"></param>
        /// <returns></returns>
        public bool CloseChannelPort(int channel)
        {
            try
            {
                UiBLL.RichTextBoxAppendText(_RichTextBoxInfo, Brushes.Blue, "断开通道" + channel + "串口继电器");
                List<string> sendstr = new List<string>();
                switch (channel)
                {
                    case 1: sendstr.Add(PortModel.RelayUartModel.Off1); sendstr.Add(PortModel.RelayUartModel.Off2); break;
                    case 2: sendstr.Add(PortModel.RelayUartModel.Off4); sendstr.Add(PortModel.RelayUartModel.Off5); break;
                    case 3: sendstr.Add(PortModel.RelayUartModel.Off7); sendstr.Add(PortModel.RelayUartModel.Off8); break;
                    case 4: sendstr.Add(PortModel.RelayUartModel.Off10); sendstr.Add(PortModel.RelayUartModel.Off11); break;
                    case 5: sendstr.Add(PortModel.RelayUartModel.Off13); sendstr.Add(PortModel.RelayUartModel.Off14); break;
                    default:
                        break;
                }
                //依次断开继电器
                foreach (var item in sendstr)
                {
                    string recievestr = SendGetStr(item);
                    if (recievestr.Contains(PortModel.RelayUartModel.Ok) == false)
                    {
                        UiBLL.RichTextBoxAppendText(_RichTextBoxInfo, Brushes.Tomato, "断开通道" + channel + "串口继电器失败");
                        return false;
                    }
                }

                UiBLL.RichTextBoxAppendText(_RichTextBoxInfo, Brushes.Blue, "断开通道" + channel + "串口继电器成功");
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 闭合通道的3个继电器
        /// </summary>
        /// <param name="channel"></param>
        /// <returns></returns>
        public bool OpenChannel(int channel)
        {
            try
            {
                UiBLL.RichTextBoxAppendText(_RichTextBoxInfo, Brushes.Blue, "闭合通道" + channel + "继电器");
                List<string> sendstr = new List<string>();
                switch (channel)
                {
                    case 1: sendstr.Add(PortModel.RelayUartModel.On1); sendstr.Add(PortModel.RelayUartModel.On2); sendstr.Add(PortModel.RelayUartModel.On3); break;
                    case 2: sendstr.Add(PortModel.RelayUartModel.On4); sendstr.Add(PortModel.RelayUartModel.On5); sendstr.Add(PortModel.RelayUartModel.On6); break;
                    case 3: sendstr.Add(PortModel.RelayUartModel.On7); sendstr.Add(PortModel.RelayUartModel.On8); sendstr.Add(PortModel.RelayUartModel.On9); break;
                    case 4: sendstr.Add(PortModel.RelayUartModel.On10); sendstr.Add(PortModel.RelayUartModel.On11); sendstr.Add(PortModel.RelayUartModel.On12); break;
                    case 5: sendstr.Add(PortModel.RelayUartModel.On13); sendstr.Add(PortModel.RelayUartModel.On14); sendstr.Add(PortModel.RelayUartModel.On15); break;
                    default:
                        break;
                }
                //依次闭合继电器
                foreach (var item in sendstr)
                {
                    string recievestr = SendGetStr(item);
                    if (recievestr.Contains(PortModel.RelayUartModel.Ok) == false)
                    {
                        UiBLL.RichTextBoxAppendText(_RichTextBoxInfo, Brushes.Blue, "闭合通道" + channel + "继电器失败");
                        return false;
                    }
                }
                UiBLL.RichTextBoxAppendText(_RichTextBoxInfo, Brushes.Blue, "闭合通道" + channel + "继电器完成");
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 断开通道3个继电器
        /// </summary>
        /// <param name="channel"></param>
        /// <returns></returns>
        public bool CloseChannel(int channel)
        {
            try
            {
                UiBLL.RichTextBoxAppendText(_RichTextBoxInfo, Brushes.Blue, "断开通道" + channel+"继电器");
                List<string> sendstr = new List<string>();
                switch (channel)
                {
                    case 1: sendstr.Add(PortModel.RelayUartModel.Off1); sendstr.Add(PortModel.RelayUartModel.Off2); sendstr.Add(PortModel.RelayUartModel.Off3); break;
                    case 2: sendstr.Add(PortModel.RelayUartModel.Off4); sendstr.Add(PortModel.RelayUartModel.Off5); sendstr.Add(PortModel.RelayUartModel.Off6); break;
                    case 3: sendstr.Add(PortModel.RelayUartModel.Off7); sendstr.Add(PortModel.RelayUartModel.Off8); sendstr.Add(PortModel.RelayUartModel.Off9); break;
                    case 4: sendstr.Add(PortModel.RelayUartModel.Off10); sendstr.Add(PortModel.RelayUartModel.Off11); sendstr.Add(PortModel.RelayUartModel.Off12); break;
                    case 5: sendstr.Add(PortModel.RelayUartModel.Off13); sendstr.Add(PortModel.RelayUartModel.Off14); sendstr.Add(PortModel.RelayUartModel.Off15); break;
                    default:
                        break;
                }
                //依次断开继电器
                foreach (var item in sendstr)
                {
                    string recievestr = SendGetStr(item);
                    if (recievestr.Contains(PortModel.RelayUartModel.Ok) == false)
                    {
                        UiBLL.RichTextBoxAppendText(_RichTextBoxInfo, Brushes.Tomato, "断开通道" + channel + "继电器失败");
                        return false;
                    }
                }

                UiBLL.RichTextBoxAppendText(_RichTextBoxInfo, Brushes.Blue, "断开通道" + channel + "继电器成功");
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 断开所有通道继电器
        /// </summary>
        /// <returns></returns>
        public bool CloseAll()
        {
            try
            {
                UiBLL.RichTextBoxAppendText(_RichTextBoxInfo, Brushes.Blue, "断开所有通道继电器成功");
                string recievestr = SendGetStr(BtLabelModel.PortModel.RelayUartModel.AllOff);
                if (recievestr.Contains(PortModel.RelayUartModel.Ok) == false)
                {
                    UiBLL.RichTextBoxAppendText(_RichTextBoxInfo, Brushes.Blue, "断开所有通道继电器成功");
                    return false;
                }
                UiBLL.RichTextBoxAppendText(_RichTextBoxInfo, Brushes.Blue, "断开所有通道继电器成功");
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
